package com.lzx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzx.api.CommonResult;
import com.lzx.dao.repository.ProductRepository;
import com.lzx.dto.ProductDto;
import com.lzx.entity.Collect;
import com.lzx.entity.Product;
import com.lzx.service.CollectService;
import com.lzx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ListResourceBundle;
import java.util.stream.Collectors;

/**
 * (Collect)表控制层
 *
 * @author makejava
 * @since 2021-04-06 17:22:49
 */
@RestController
@RequestMapping("collect")
public class CollectController {
    /**
     * 服务对象
     */
    @Autowired
    private CollectService collectService;

    @Autowired
    private ProductRepository productRepository;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */

    @GetMapping("selectOne")
    public Collect selectOne(Integer id) {
        return this.collectService.queryById(id);
    }

    @PostMapping("/add")
    public CommonResult addToCollect(Integer memberId,Integer productId){
        if (BeanUtil.isEmpty(memberId)||BeanUtil.isEmpty(productId)) {
            return CommonResult.failed("请先登录");
        }
        HashMap<String, Integer> queryMap = new HashMap<>(2);
        queryMap.put("memberid",memberId);
        queryMap.put("productid",productId);

        Collect one = collectService.getOne(new QueryWrapper<Collect>().allEq(queryMap));
        if (BeanUtil.isNotEmpty(one)) {
            return CommonResult.failed("该商品已存在收藏列表中");
        }
        Collect collect = new Collect(null, memberId, productId, new Date());
        boolean result = collectService.save(collect);
        return CommonResult.success(result,"添加收藏成功！");
    }
    @GetMapping("list")
    public CommonResult list(Integer memberId){
        if (BeanUtil.isEmpty(memberId)) {
            return CommonResult.failed();
        }

        List<Collect> resultList = collectService.list(new QueryWrapper<Collect>().eq("memberid", memberId));
        List<Integer> productIds = resultList.stream().map(Collect::getProductid).collect(Collectors.toList());
        Iterable<ProductDto> productDtos = productRepository.findAllById(productIds);


        return CommonResult.success(productDtos);
    }
    @DeleteMapping("remove")
    public CommonResult remove(Integer memberId,Integer productId){
        if (BeanUtil.isEmpty(memberId)||BeanUtil.isEmpty(productId)) {
            return CommonResult.failed("删除失败，重试");
        }
        HashMap<String, Integer> queryMap = new HashMap<>(2);
        queryMap.put("memberid",memberId);
        queryMap.put("productid",productId);

        Collect one = collectService.getOne(new QueryWrapper<Collect>().allEq(queryMap));
        collectService.removeById(one.getId());
        return CommonResult.success(200,"删除成功");
    }

}
